package com.qianke.bops.controller;

import com.qianke.common.Assert;
import com.qianke.common.RequestContext;
import com.qianke.common.util.PasswordUtil;
import com.qianke.core.service.AdminMenuService;
import com.qianke.core.service.AdminUserService;
import com.qianke.domain.ApiResponse;
import com.qianke.domain.BizConstants;
import com.qianke.domain.Constants;
import com.qianke.domain.dto.AdminUserDto;
import com.qianke.domain.dto.LoginUserDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin")
public class AdminUserController {

    @Resource
    private AdminUserService adminUserService;
    @Resource
    private AdminMenuService adminMenuService;

    @RequestMapping("/doLogin")
    public ApiResponse doLogin(String account, String password, @RequestParam(defaultValue = "false") boolean remember) {
        System.out.println(999);
        Assert.assertNotNull(account, "账号不能为空!");
        Assert.assertNotNull(password, "密码不能为空!");

        AdminUserDto adminUser = adminUserService.queryAdminUserByAccount(account);

        Assert.assertNotNull(adminUser, BizConstants.ACCOUNT_NOT_EXIST_ERROR);
        String encodePass = PasswordUtil.encode(password, adminUser.getSalt());
        Assert.assertNotEquals(adminUser.getPassword(), encodePass, BizConstants.PASSWORD_ERROR);
        LoginUserDto loginUserDto = new LoginUserDto();
        loginUserDto.setId(adminUser.getId());
        loginUserDto.setAccount(adminUser.getAccount());
        loginUserDto.setMobile(adminUser.getMobile());
        loginUserDto.setNickname(adminUser.getNickname());
        loginUserDto.setRoleCode(adminUser.getRoleCode());
        if (Constants.ADMIN_ACCOUNT.contains(adminUser.getAccount())) {
            loginUserDto.setPermissions("all");
        } else if (StringUtils.isNotEmpty(adminUser.getMenuIds())) {
            loginUserDto.setPermissions(adminMenuService.queryAdminMenuList(adminUser.getRoleCode(), adminUser.getMenuIds()));
        }

        if (remember) {
            loginUserDto.setPassword(encodePass);
        }

        RequestContext.setLoginUser(loginUserDto);
        return ApiResponse.successApiResponse(null);
    }

//    @RequestMapping("/insert")
//    public ApiResponse insert(AdminUserDto adminUserDto) {
//        Assert.assertNotNull(adminUserDto);
//        generatePassword(adminUserDto);
//        return new ApiResponse(adminUserService.insert(adminUserDto));
//    }
//
//    @RequestMapping("/update")
//    public ApiResponse update(AdminUserDto adminUserDto) {
//        Assert.assertNotNull(adminUserDto);
//        Assert.assertNotNull(adminUserDto.getId());
//        generatePassword(adminUserDto);
//        return new ApiResponse(adminUserService.update(adminUserDto));
//    }
//
//    private void generatePassword(AdminUserDto adminUserDto) {
//        if (adminUserDto == null) return;
//        String password = adminUserDto.getPassword();
//        if (StringUtils.isNotEmpty(password)) {
//            String salt = RandomStringUtils.randomAlphabetic(4);
//            adminUserDto.setSalt(salt);
//            adminUserDto.setPassword(PasswordUtil.encode(password, salt));
//        }
//    }
}
